﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 品牌维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改品牌维护在一个页面。
/// </summary>
public partial class masterdata_Brand_Type_List : PageBase
{
    BrandManager manager = new BrandManager();
    Jumpton.Persistent.ObjectTable ot;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetManuType();
            //btnAdd0
            GetCategoryType();
            GetBrandsBind();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetBrandsBind()
    {
        DataSet ds = BrandManager.GetAllBrandsListDaynamic(ddlCategroy.SelectedValue,
                                                                ddlManu.SelectedValue,
                                                                MyStateManager.DomainID,
                                                                ddlValid.SelectedValue);

        GridPager1.DataSource = ds.Tables[0];
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;
        
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    
    }

    #region 绑定制造商
    public void GetManuType()
    {
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=TRUE";
            ddlManu.DataSource = ot.BillDataTable.DefaultView;
            ddlManu.DataBind();
        }
        ddlManu.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    #region 绑定品类
    public void GetCategoryType()
    {
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ot = CategoryManager.GetCategories(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=TRUE";
            ddlCategroy.DataSource = ot.BillDataTable.DefaultView;
            ddlCategroy.DataBind();
        }
        ddlCategroy.Items.Insert(0, new ListItem("--所有--", ""));
    }
    #endregion

    /// <summary>
    /// 根据条件数据查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        GetBrandsBind();
    }

    /// <summary>
    /// 新增方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
     
    }

   

    /// <summary>
    /// 保存数据方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            BRANDS brand = null;

            TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox ctl = DetailsView1.FindControl("txtName") as TextBox;
            DropDownList ddlCategoryType = DetailsView1.FindControl("ddlCategory") as DropDownList;
            DropDownList ddlManuType = DetailsView1.FindControl("ddlManu") as DropDownList;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                brand = new BRANDS();
                brand.CREATETIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
                //判断是否存在此代码
                if (manager.LoadWithDomainID(ctlcode.Text, MyStateManager.DomainID) != null)
                {
                    ShowMessage("已存在此代码");
                    return;
                }
                //判断是否存在此名称
                if (manager.LoadName(ctl.Text, MyStateManager.DomainID) != null)
                {
                    ShowMessage("已存在此名称");
                    return;
                }
            }
            else
            {
                BRANDS c = manager.LoadName(ctl.Text, MyStateManager.DomainID);
                //判断是否已经存在此名称
                if (c != null)
                {
                    if (!c.BrandCode.Equals(ctlcode.Text))
                    {
                        ShowMessage("已存在此名称");
                        return;
                    }
                }
                brand = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
            brand.BrandCode = ctlcode.Text;
            brand.BrandName = ctl.Text;
            brand.CategoryCode = ddlCategoryType.SelectedValue;
            brand.ManuCode = ddlManuType.SelectedValue;
            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            brand.ACTIVE = cbxValid.Checked;
            brand.DOMAINID = MyStateManager.DomainID;
            brand.LAST_UPDATER = MyStateManager.UserID.ToString();
            brand.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));


            string LogContent = "";

            if (brand.State == DataRowState.Added)
            {
                LogContent += "增加新的品牌，" + ctlcode.Text + "的信息是：";
            }
            else
            {
                LogContent += brand.BrandCode + "信息发生变动，变动后信息是：";
            }
            LogContent += "品牌代码：" + brand.BrandCode + "，";
            LogContent += "品牌名称：" + brand.BrandName + "，";
            LogContent += "制造商名称：" + ddlManuType.SelectedItem.Text + "，";
            LogContent += "品类名称：" + ddlCategoryType.SelectedItem.Text + "，";
            LogContent += "有效：" + brand.ACTIVE + "，";
            LogContent += "操作人：" + brand.LAST_UPDATER;

            if (manager.Save(brand))
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                 Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                 this.MyStateManager.UserID, LogContent);
                if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
                {
                    GetBrandsBind();
                }
                else
                {
                    Response.Redirect(Request.RawUrl.ToString());
                }
            }
           
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                   Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                   this.MyStateManager.UserID, "包装维护 - 异常:" + ex.Message);
        }
    }

   

    /// <summary>
    /// 编辑时数据绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlCategoryType = DetailsView1.FindControl("ddlCategory") as DropDownList;
        DropDownList ddlManuType = DetailsView1.FindControl("ddlManu") as DropDownList;
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;
        TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
        if (ddlManuType != null)
        {
            ddlManuType.DataTextField = "ManuName";
            ddlManuType.DataValueField = "ManuCode";
            ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
            if (ot != null)
            {
                ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=TRUE";
                ddlManuType.DataSource = ot.BillDataTable.DefaultView;
                ddlManuType.DataBind();
            }
            ddlManuType.Items.Insert(0, new ListItem("--所有--", ""));

            ddlCategoryType.DataTextField = "CategoryName";
            ddlCategoryType.DataValueField = "CategoryCode";
            ot = CategoryManager.GetCategories(MyStateManager.DomainID);
            if (ot != null)
            {
                ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
                ddlCategoryType.DataSource = ot.BillDataTable.DefaultView;
                ddlCategoryType.DataBind();
            }
            ddlCategoryType.Items.Insert(0, new ListItem("--所有--", ""));

            if (DetailsView1.DataItem != null)
            {
                ddlManuType.SelectedValue = ((BRANDS)DetailsView1.DataItem).ManuCode.ToString();
                ddlCategoryType.SelectedValue = ((BRANDS)DetailsView1.DataItem).CategoryCode.ToString();
                ctlcode.Enabled = false;
            }
            else
            {
                chk.Checked = true;
            }
        }
    }

    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        
        DetailsView1.DataBind();
       
    }
    //protected void btnAdd0_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("/Server/ExportExcel/ExportExcel.aspx?Table=" + "Brands" + "&Category=" + ddlCategroy.SelectedValue + "&Manu=" + ddlManu.SelectedValue + "&Valid=" + ddlValid.SelectedValue);
    //}

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetBrandsBind();
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
}
